﻿using System.Linq;
using EnvDTE;

namespace AzurePlugin.Converter
{
    public class ProjectConfigurationManagerAdapter
    {
        private readonly ConfigurationManager _configurationManager;

        public ProjectConfigurationManagerAdapter(ConfigurationManager configurationManager)
        {
            _configurationManager = configurationManager;
        }

        public Configuration GetConfiguration(PlatformTarget platformTarget)
        {
            foreach (Configuration configuration in _configurationManager)
            {
                var target = GetStringPropertyValue(configuration, "PlatformTarget");
                if (target == platformTarget.ToString())
                    return configuration;
            }

            return null;
        }

        public string GetStringPropertyValue(Configuration configuration, string name)
        {
            foreach (Property property in configuration.Properties)
            {
                if (property.Name == name)
                    return property.Value.ToString();
            }

            return string.Empty;
        }

        public void SetStringPropertyValue(Configuration configuration, string name, string value)
        {
            foreach (Property property in configuration.Properties)
            {
                if (property.Name == name)
                    property.Value = value;
            }
        }

        public bool ContainsPlatform(string plaformName)
        {
            return ((object[]) _configurationManager.PlatformNames).Any(x => x.ToString() == plaformName);
        }

        public void AddPlaform(string newPlaformName, string clonePlaformName)
        {
            _configurationManager.AddPlatform(newPlaformName, clonePlaformName, true);
        }
    }
}
